#include "objsaver.h"
#include <GL/glut.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;

void objsave(vector<vector<GLfloat>> &vSets, vector<vector<GLfloat>> &vtSets,
             vector<vector<GLfloat>> &vnSets, vector<vector<fpoint>> &fSets) {

    cout << "saving obj file" << endl;

    FILE *pWritingFile; //指向要保存截图的bmp文件

    //生成时间+随机数的文件名
    time_t t = time(0); //获取时间作为截图文件名
    char filename[40];
    strftime(filename, sizeof(filename), "%Y%m%d%H%M%S",
             localtime(&t));      //将时间转化为固定格式
    int a = rand() % 9000 + 1000; //在时间后面添加一个四位的随机数
    char str[6];
    sprintf(str, "%d", a);
    char filepath[100];
    strcpy(filepath, FilePathObjSave);
    strcat(filename, str);
    strcat(filename, ".obj");
    strcat(filepath, filename);
    // cout << filepath << endl;

    pWritingFile = fopen(filepath, "wb"); //只写形式打开
    if (pWritingFile == 0)
        exit(0);

    //三个顶点
    GLfloat SV[3], VN[3], VT[2];

    for (std::vector<std::vector<float>>::size_type i = 0; i < vSets.size(); i++) {

        if (vSets[i].size() != 3) {
            cout << "wrong v data" << endl;
        } else {
            SV[0] = (vSets[i])[0]; //第一个顶点
            SV[1] = (vSets[i])[1];
            SV[2] = (vSets[i])[2];
            fprintf(pWritingFile, "v %f %f %f\n", SV[0], SV[1], SV[2]);
        }
    }

    for (std::vector<std::vector<float>>::size_type i = 0; i < vtSets.size(); i++) {
        if (vtSets.size() == 0) {
            cout << "no vt data" << endl;
        } else {
            if (vtSets[i].size() != 2) {
                cout << "wrong vt data" << endl;
            } else {
                VT[0] = (vtSets[i])[0]; //第一个顶点纹理
                VT[1] = (vtSets[i])[1];

                fprintf(pWritingFile, "vt %f %f\n", VT[0], VT[1]);
            }
        }
    }

    for (std::vector<std::vector<float>>::size_type i = 0; i < vnSets.size(); i++) {
        if (vnSets.size() == 0) {
            cout << "no vn data" << endl;
        } else {
            if (vnSets[i].size() != 3) {
                cout << "wrong vn data" << endl;
            } else {
                VN[0] = (vnSets[i])[0]; //第一个顶点法向量
                VN[1] = (vnSets[i])[1];
                VN[2] = (vnSets[i])[2];

                fprintf(pWritingFile, "vn %f %f %f\n", VN[0], VN[1], VN[2]);
            }
        }
    }

    fprintf(pWritingFile, "\n");

    //按照 顶点/法向量/纹理 的顺序存入一个面的三个点
    for (std::vector<std::vector<float>>::size_type i = 0; i < fSets.size(); i++) {
        if ((fSets[i])[0].vn == -1 && (fSets[i])[0].vt == -1) {

            fprintf(pWritingFile, "f %i %i %i\n", ((fSets[i])[0]).v + 1,
                    ((fSets[i])[1]).v + 1, ((fSets[i])[2]).v + 1);
        } else if ((fSets[i])[0].vn != -1 && (fSets[i])[0].vt == -1) {

            fprintf(pWritingFile, "f %i//%i %i//%i %i//%i\n",
                    ((fSets[i])[0]).v + 1, ((fSets[i])[0]).vn + 1,
                    ((fSets[i])[1]).v + 1, ((fSets[i])[1]).vn + 1,
                    ((fSets[i])[2]).v + 1, ((fSets[i])[2]).vn + 1);
        } else if ((fSets[i])[0].vn == -1 && (fSets[i])[0].vt != -1) {

            fprintf(pWritingFile, "f %i/%i %i/%i %i/%i\n",
                    ((fSets[i])[0]).v + 1, ((fSets[i])[0]).vt + 1,
                    ((fSets[i])[1]).v + 1, ((fSets[i])[1]).vt + 1,
                    ((fSets[i])[2]).v + 1, ((fSets[i])[2]).vt + 1);
        } else {

            fprintf(pWritingFile, "f %i/%i/%i %i/%i/%i %i/%i/%i\n",
                    ((fSets[i])[0]).v + 1, ((fSets[i])[0]).vt + 1,
                    ((fSets[i])[0]).vn + 1, ((fSets[i])[1]).v + 1,
                    ((fSets[i])[1]).vt + 1, ((fSets[i])[1]).vn + 1,
                    ((fSets[i])[2]).v + 1, ((fSets[i])[2]).vt + 1,
                    ((fSets[i])[2]).vn + 1);
        }
    }

    fclose(pWritingFile);
}
